<template>
  <div class="login_container">
    <!-- 登录框 -->
    <div class="login_box">
      <!-- logo -->
      <div class="login_img">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 表单 -->
      <div class="login_form">
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px">
          <!-- 登录框 -->
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              prefix-icon="iconfont icon-3702mima"
              type="password"
            ></el-input>
          </el-form-item>
          <!-- 提交按钮 -->
          <el-form-item class="btns">
            <el-button type="primary" @click="login()">登录</el-button>
            <el-button type="info" @click="reset()">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置按钮
    reset () {
      this.$refs.loginFormRef.resetFields()
    },
    // 登录按钮
    login () {
      this.$refs.loginFormRef.validate(async boolean => {
        if (!boolean) return
        // 返回promise对象。使用async和await
        const { data: res } = await this.$http.post('/login', this.loginForm)
        // 判断返回的status
        console.log(res)
        if (res.meta.status !== 200) {
          return this.$message({ 
            message: '用户名或者密码错误',
            type: 'error'
          })
        }
        this.$message({
            message: '登录成功',
            type: 'success'
        })
        // 登录成功之后的token保存在sessionstrorage中
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background-color: #2b4b66;
  .login_box {
    width: 500px;
    height: 300px;
    background-color: #fff;
    border-radius: 4px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
}
.login_img {
  width: 120px;
  height: 120px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #eee;
  background-color: #fff;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -30%);
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  //   原数据是content-box,会导致盒子挤出login框
  box-sizing: border-box;
}
</style>
